//
//  PrivateLetterViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/15.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "PrivateLetterViewController.h"
#import "DAPagesContainer.h"
#import "SessionViewController.h"
#import "ServiceViewController.h"
#import "LoginViewController.h"
@interface PrivateLetterViewController ()
@property(nonatomic,strong)SessionViewController *shopVCtrl;
@property(nonatomic,strong)ServiceViewController *indutryVCtrl;
@property(strong, nonatomic)DAPagesContainer *pagesContainer;
@property(nonatomic,strong)LoginViewController  *login;
@property(nonatomic,strong)SessionViewController *shopVCtrl2;
@property(nonatomic,copy)NSString  *currentIndex;
@end

@implementation PrivateLetterViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    

    if(self.pagesContainer){
        self.pagesContainer.isSession = YES;
        self.pagesContainer.pageType = DAPagesContainerPrivate;
        DLog(@"当前pageType---DAPagesContainerPrivate");
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if(self.pagesContainer){
      self.pagesContainer.isSession = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.vcName = @"私信";
    
    //分段空控件
    [self daPageCtrl];
    [self initRightBarItem];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(privateVCCurrentSelectedIndex:) name:@"privateVCCurrentSelectedIndex" object:nil];

    if([AppDelegate appDealegate].uid.length){//如果已经登录则到此为止
        return;
    }
    //如果为登录继续
    [self addLoginView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginSuccess) name:@"loginSuccess" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(logOutSuccess) name:@"logOutSuccess" object:nil];
    }
-(void)privateVCCurrentSelectedIndex:(NSNotification*)noti{
    self.currentIndex = noti.object;
}

-(void)addLoginView{
    LoginViewController *login = [[LoginViewController alloc]init];
    login.isNotNeedBackBtn = YES;
    self.login = login;
    [self.navigationController pushViewController:login animated:NO];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)loginSuccess{
    if(self.login!=nil){
       [self.login.navigationController popViewControllerAnimated:NO];
        self.login = nil;
    }
}
-(void)logOutSuccess{
    [self addLoginView];
}

- (void)initRightBarItem{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame=CGRectMake(0, 0, 70, 40);
    [rightButton setTitle:@"忽略未读" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton setTitleColor:DEF_COLOR  forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(right_Action) forControlEvents:UIControlEventTouchUpInside];
    self.rightItem = rightButton;
}

-(void)right_Action{
    if([self.currentIndex isEqualToString:@"1"]){
        DLog(@"忽略服务号");
          [[NSNotificationCenter defaultCenter]postNotificationName:@"ignoreService" object:nil];
      
    }else{
        DLog(@"忽略会话");
        [self.shopVCtrl ignoreAllUnreadSessionMessage];
    }
}


- (void)daPageCtrl{
    NSMutableArray *controllersArray = [NSMutableArray array];
    self.shopVCtrl = [[SessionViewController alloc]init];
    self.shopVCtrl.masterViewControllerForPush = self;
    self.shopVCtrl.isPush = self.isPush;
    self.shopVCtrl.title = @"会话";
    [controllersArray addObject:self.shopVCtrl];
    self.indutryVCtrl = [[ServiceViewController alloc]init];
    self.indutryVCtrl.masterViewControllerForPush = self;
    self.indutryVCtrl.title = @"服务号";
    [controllersArray addObject:self.indutryVCtrl];
    [self setUpPagesContainersWithChildControllers:controllersArray];
}
-(void)setUpPagesContainersWithChildControllers:(NSArray *)controllers {
    self.pagesContainer = [[DAPagesContainer alloc] init];
    [self.pagesContainer willMoveToParentViewController:self];
    self.pagesContainer.view.frame = CGRectMake(0, kNaviBarHeight, Main_Screen_Width, Main_Screen_Height-kBottomBarHeight-kNaviBarHeight);
    self.pagesContainer.pageIndicatorColor = DEF_COLOR;
    self.pagesContainer.pageItemsTitleColor = [UIColor blackColor];
    self.pagesContainer.topBarBackgroundColor = [UIColor whiteColor];
    self.pagesContainer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.pagesContainer.topBarHeight = 44;
    [self.view addSubview:self.pagesContainer.view];
    [self.pagesContainer didMoveToParentViewController:self];
    self.pagesContainer.viewControllers = controllers;
    self.pagesContainer.pageType = DAPagesContainerPrivate;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
